package cn.wolfcode.car.controller;

import cn.wolfcode.car.domain.SystemDictionary;
import cn.wolfcode.car.domain.SystemDictionaryItem;
import cn.wolfcode.car.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.car.service.ISystemDictionaryItemService;
import cn.wolfcode.car.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import result.JsonResult;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;



    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo) {
        //列表数据
        PageInfo<SystemDictionaryItem> list = systemDictionaryItemService.list(qo);
        model.addAttribute("result", list);
        //字典大类
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("systemDictionaries", systemDictionaries);
        //小类
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.listAll();
        model.addAttribute("systemDictionaryItems", systemDictionaryItems);
        return "systemDictionaryItem/list";
    }

    @PostMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        System.out.println(systemDictionaryItem);
        if (systemDictionaryItem.getId() == null) {
            systemDictionaryItemService.save(systemDictionaryItem);
        } else {
            systemDictionaryItemService.update(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @GetMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        systemDictionaryItemService.delete(id);
        return JsonResult.success(null);
    }
}
